<?php

namespace app\models;

use Yii;
use app\models\events\UploadEvent;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
/**
 * This is the model class for table "sheet".
 *
 * @property integer $id
 * @property string $name
 * @property string $brief_desc
 * @property string $pic_url
 * @property integer $user_id
 * @property integer $is_like
 * @property integer $created_at
 * @property integer $updated_at
 */
class Sheet extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sheet';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'brief_desc', 'pic_url', 'user_id'], 'required'],
            [['name', 'brief_desc', 'pic_url'], 'string', 'max' => 255],
            ['is_like','default','value'=>0],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'id'),
            'name' => Yii::t('app', 'name'),
            'brief_desc' => Yii::t('app', '描述'),
            'pic_url' => Yii::t('app', '备注'),
            'user_id' => Yii::t('app', '用户id'),
            'is_like' => Yii::t('app', '喜欢的歌单'),
            'created_at' => Yii::t('app', '创建时间'),
            'updated_at' => Yii::t('app', '修改时间'),
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes'=>[
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class'=>UploadEvent::className(),
                'changeAttribute'=>'pic_url',
            ]
        ];
    }

    public function getSongs()
    {
        return $this->hasMany(Song::className(),['id'=>'song_id'])
            ->viaTable('acs_song_sheet',['sheet_id'=>'id']);
    }
}
